package com.fortune.rainshine.controller;

import com.fortune.rainshinecommon.alioss.OssUtils;
import com.fortune.rainshinecommon.base.AliOssParmsModel;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.enums.AliOssTypeEnum;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.util.OssFileNameUtils;
import com.fortune.rainshinecommon.util.PicUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.UUID;

/**
 * @program: rainShine
 * @description:
 * @author: tangyn
 * @create: 2019-06-04
 **/
@RestController
@RequestMapping("fileManage")
public class FileManageController {
    private static final Logger logger = LoggerFactory.getLogger(FileManageController.class);

//    @Autowired
//    private AliOssService aliOssService;

    @ResponseBody
    @RequestMapping("/fileUpload")
    public MsgModel fileUpload(@RequestBody MultipartFile file) {
        try {
            if(file == null){
                return new MsgModel("请选择文件", ResultCode.FAILURE);
            }
            String fileName = file.getOriginalFilename();
            if(fileName == null || !fileName.contains(".")){
                return new MsgModel("文件格式不正确", ResultCode.FAILURE);
            }
            String prefix = fileName.substring(fileName.lastIndexOf('.')).toLowerCase();
            String[] picTypes = {".jpg",".bmp",".png",".jpeg"};
            if (!Arrays.asList(picTypes).contains(prefix)) {
                return new MsgModel("请上传jpg/bmp/png/jpeg格式的图片!", ResultCode.FAILURE);
            }
            if(file.getSize()/1024 > (double) 500){
                return new MsgModel("上传图片不能大于500KB!", ResultCode.FAILURE);
            }
            String floder = AliOssTypeEnum.getValue(1);
            String rannum = UUID.randomUUID().toString().replaceAll("-", "");

            String newName = OssFileNameUtils.getNewName(file, floder, rannum);
            byte[] bytes = PicUtils.quality(file.getBytes(), 0.2f);
            AliOssParmsModel model = new AliOssParmsModel(rannum, floder, newName, bytes);
//            aliOssService.uploadFile(model);
            String filePath = "shengxian/" + floder + "/" + newName;
            String mkdir = "shengxian/" + floder + "/";
            OssUtils.uploadFile(mkdir, filePath, bytes);

            String path = OssFileNameUtils.getFilePath(floder, newName);
//            String filepath = aliOssService.getUrl(path);
            String filepath = OssUtils.getUrl(path);
            return new MsgModel(ResultCode.SUCCESS, filepath);
        } catch (Exception e) {
            logger.error("fileUpload ====> {}", e);
            return new MsgModel(ResultCode.FAILURE);
        }
    }
}
